package com.investmenttechnologyservices.model;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;

public class Instrument extends EntityHolder {
//	private String key; // usually ISIN but for non-listed will be generated with pattern @+Id - use prepersist
//	private String firmKey; // NULL indicates it is globally available to all firms, otherwise specific to firm
//	private String name;
//	private Key issuerKey; // can be NULL
//	Next two values are set at the Instrumen Type level as well. They can be overridden at the instrument level.
//	private Boolean cashTraded ;
//	private Boolean isListed ;
//	private PriceFormat priceFormat;
	
	public Instrument(Entity entity) {
		super(entity);
	}
	
	public Instrument(String id) {
	    super("instrument", id);
	}

	public Instrument(String id, String name, Key issuerKey, InstrumentType instrumentType, 
			Boolean isListed, Boolean cashTraded, Key firmKey, PriceFormat priceFormat) {
	    this(id);
		setName(name);
		setIssuerKey(issuerKey);
		setInstrumentType(instrumentType);
		setIsListed(isListed); 
		setCashTraded(cashTraded); 
		setFirmKey(firmKey);
		setPriceFormat(priceFormat);
	}
	
	public Instrument(String id, Key firmKey, String name, Key issuerKey, InstrumentType instrumentType) {
	    this(id, name, issuerKey, instrumentType, null, null, firmKey, PriceFormat.SIMPLE);
	}
	
	public Key getKey() {
		return super.getKey();}
	
	public Key getFirmKey() {
		return (Key) getProperty("firmKey");
	}

	public void setFirmKey(Key firmKey) {
		setProperty("firmKey", firmKey);
	}

	public String getName() {
		return (String) getProperty("name");
	}

	public void setName(String name) {
		setProperty("name", name);
	}

	public Key getIssuerKey() {
		return (Key)getProperty("issuerKey");
	}

	public void setIssuerKey(Key issuerKey) {
		setProperty("issuerKey", issuerKey);
	}

	public Boolean getIsListed() {
		return (Boolean)getProperty("isListed");
	}

	public void setIsListed(Boolean isListed) {
		setProperty("isListed", isListed);
	}
	
	public Boolean getCashTraded() {
		return (Boolean)getProperty("cashTraded");
	}

	public void setCashTraded(Boolean cashTraded) {
		setProperty("cashTraded", cashTraded);
	}

	public InstrumentType getInstrumentType() {
		String storedValue = (String) getProperty("instrumentType");
		if (storedValue == null) {
			return InstrumentType.EQUITY;
		} else {
			return InstrumentType.valueOf(storedValue);
		}
	}
	
	public void setInstrumentType(InstrumentType instrumentType){
		if (instrumentType == null || instrumentType == InstrumentType.EQUITY) {
			setProperty("instrumentType",null, true);
		} else {
			setProperty("instrumentType",instrumentType.name());
		}
	}

	public PriceFormat getPriceFormat() {
		String storedValue = (String) getProperty("priceFormat");
		if (storedValue == null) {
			return PriceFormat.SIMPLE;
		} else {
			return PriceFormat.valueOf(storedValue);
		}
	}
	
	public void setPriceFormat(PriceFormat priceFormat){
		if (priceFormat == null || priceFormat == PriceFormat.SIMPLE) {
			setProperty("priceFormat",null, true);
		} else {
			setProperty("priceFormat",priceFormat.name());
		}
	}


//	@Override
//	public String toString() {
//		return "Instrument [key=" + key + ", firmId=" + firmId + ", name="
//				+ name + ", issuerKey=" + issuerKey + ", cashTraded="
//				+ cashTraded + "]";
//	}

}
